/*
 * @Author: your name
 * @Date: 2020-04-29 09:04:13
 * @LastEditTime: 2020-04-29 10:18:48
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \knowledge\nodejs\learning\index.js
 */
 //require _ from 'lodash.js'
let _ = require('lodash')
let group = {"groupOp":"OR","rules":[{"field":"Name","op":"cn","data":"111","fieldType":1,"labelName":"姓名","opName":"包含","valueName":"111"},{"field":"Sex","op":"eq","data":"f0f445cf-d2d5-4538-b35f-a98001737e3c","fieldType":0,"labelName":"性别","sourceList":[{"value":"d0613810-0b1b-4c59-9055-a98001737644","label":"男"},{"value":"f0f445cf-d2d5-4538-b35f-a98001737e3c","label":"女"}],"opName":"等于","valueName":"女"},{"field":"Birthday","op":"lt","data":"2020-04-26T16:00:00.000Z","fieldType":2,"labelName":"出生年月","opName":"小于","valueName":"2020/4/27"}],"groups":[{"groupOp":"OR","rules":[{"field":"Sex","op":"eq","data":"f0f445cf-d2d5-4538-b35f-a98001737e3c","fieldType":0,"sourceList":[{"value":"d0613810-0b1b-4c59-9055-a98001737644","label":"男"},{"value":"f0f445cf-d2d5-4538-b35f-a98001737e3c","label":"女"}],"sourceType":0,"labelName":"性别","opName":"等于","valueName":"女"}],"Name":"或者"}],"Name":"或者"}

function tree2Array(treeObj, rootid) {
    const temp = [];  // 设置临时数组，用来存放队列
    const out = [];    // 设置输出数组，用来存放要输出的一维数组
    temp.push(treeObj);
    // 首先把根元素存放入out中
    let pid = rootid;
    const obj = deepCopy(treeObj);
    obj.pid = pid;
    
    out.push(obj['rules'])
    // delete obj['rules'];
    // 对树对象进行广度优先的遍历
    while(temp.length > 0) {
        const first = temp.shift();
        const children = first.groups;
        if(children && children.length > 0) {
            pid = first.Name;
            // out.push({labelName:pid,opName:'',valueName:''})
            const len = first.groups.length;
            for(let i=0;i<len;i++) {
                temp.push(children[i]);
                const obj = deepCopy(children[i]);
                obj.pid = pid;
                // delete obj['rules'];
                out.push(obj['rules'])
            }
        } 
    }
    return out
 }
 
 // 深拷贝
 function deepCopy(obj){
     // 深度复制数组
     if(Object.prototype.toString.call(obj) === "[object Array]"){    
       const object=[];
       for(let i=0;i<obj.length;i++){
         object.push(deepCopy(obj[i]))
       }   
       return object
     }
     // 深度复制对象
     if(Object.prototype.toString.call(obj) === "[object Object]"){   
       const object={};
       for(let p in obj){
         object[p]=obj[p]
       }   
       return object
     }
 }


 
 let nList = tree2Array(group, 'OR');
 let sList = _.flattenDepth(nList);
 let mapList = sList.map(p=> p.labelName+p.opName+p.valueName);
let strList =mapList.join(',');
strList.trim(',')
 
 
 